package com.gable.exhibition.thread;

import com.gable.exhibition.dao.RepairDao;
import com.gable.exhibition.dao.UpkeepDao;
import com.gable.exhibition.dao.UserDao;
import com.gable.exhibition.util.HttpHelper;
import com.gable.exhibition.view.SmsView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 修改指派工程师短信
 * Created by mj on 2018/3/30.
 */
public class UpdateEngineerSmsThread implements Runnable{

    Logger logger = LoggerFactory.getLogger(this.getClass());

    private Long resourcesId;

    private Integer resourcesType;      //数据类型 1:报修，2：保养

    private RepairDao repairDao;

    private UpkeepDao upkeepDao;

    private String repairByDoctorUrl;

    private String upkeepByDoctorUrl;

    public UpdateEngineerSmsThread(Long resourcesId, Integer resourcesType, RepairDao repairDao,
                                   UpkeepDao upkeepDao,String repairByDoctorUrl,String upkeepByDoctorUrl){
        this.resourcesId = resourcesId;
        this.resourcesType = resourcesType;
        this.repairDao = repairDao;
        this.upkeepDao = upkeepDao;
        this.repairByDoctorUrl = repairByDoctorUrl;
        this.upkeepByDoctorUrl = upkeepByDoctorUrl;
    }

    @Override
    public void run() {
        try {
            SmsView smsView = null;
            String engineerUrl = "";
            if(resourcesType == 1){
                smsView = repairDao.repairForSms(resourcesId);
                engineerUrl = repairByDoctorUrl;
            }else if(resourcesType == 2){
                smsView = upkeepDao.upkeepForSms(resourcesId);
                engineerUrl = upkeepByDoctorUrl;
            }

            String hospitalName = smsView.getHospitalName();
            String deviceName = smsView.getDeviceName();
            String engineerMobile = smsView.getEngineerMobile();

            if(!StringUtils.isEmpty(engineerMobile)){
                try {
                    String message = hospitalName+"%&"+deviceName;
                    Map<String,String> map = new HashMap<String,String>();
                    map.put("mobile",engineerMobile);
                    map.put("message",message);
                    String jsonResult = HttpHelper.postRequestResponseBodyAsString(engineerUrl,map);
                    logger.info("维修指派给工程师的短信，设备名称："+deviceName+",短信发送结果:"+jsonResult);
                } catch (Exception e) {
                    logger.info("维修指派给工程师的短信，设备名称："+deviceName+",短信发送失败:"+e);
                }
            }
        } catch (Exception e) {
            logger.info("指派短信异常："+e);
        }
    }
}
